/*
 * Copyright (C) 2014 Nathan Templon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package function;

import static util.Util.doubleEquals;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import exception.FunctionDomainException;

/**
 *
 * @author Nathan Templon
 */
public class SingleVariableTableFunction implements SingleVariableFunction {
    
    public SingleVariableTableFunction(Variable var) {
        this.vars[0] = var;
    }
    
    
    public SingleVariableTableFunction(Variable var, FuncPoint[] points) {
        this.vars[0] = var;
        this.addPoints(points);
    }
    
    
    @Override
    public Variable[] getVariables() {
        return vars;
    }
    
    
    @Override
    public double getValue() {
        double value = vars[0].getValue();
        
        if (doubleEquals(domain.max(), value)) {
            return points.get(points.size() - 1).y();
        }
        else if (doubleEquals(domain.min(), value)) {
            return points.get(0).y();
        }
        
        if (!inDomain(value)) {
            throw new FunctionDomainException("The value " + value + " is not inside of the domain [" + domain.min() + ", " + domain.max() + "].");
        }
        int lowerIndex = indexOfPointSmallerThan(value);
        
        FuncPoint leftPoint = points.get(lowerIndex);
        FuncPoint rightPoint = points.get(lowerIndex + 1);
        
        double interpolationR = (value - leftPoint.x()) / (rightPoint.x() - leftPoint.x());
        double change = (rightPoint.y() - leftPoint.y());
        
        return interpolationR * change + leftPoint.y();
    }
    
    
    public final void addPoints(FuncPoint[] points) {
        this.points.addAll(Arrays.asList(points));
        Collections.sort(this.points, new Comparator<FuncPoint>() {
            @Override
            public int compare(FuncPoint p1, FuncPoint p2) {
                return Double.compare(p1.x(), p2.x());
            }
        });
        computeSpan();
    }
    
    
    public final void addPoint(FuncPoint point) {
        points.add(point);
        Collections.sort(this.points, new Comparator<FuncPoint>() {
            @Override
            public int compare(FuncPoint p1, FuncPoint p2) {
                return Double.compare(p1.x(), p2.x());
            }
        });
        computeSpan();
    }
    
    
    public final void addPoints(Collection<FuncPoint> points) {
        this.points.addAll(points);
        Collections.sort(this.points, new Comparator<FuncPoint>() {
            @Override
            public int compare(FuncPoint p1, FuncPoint p2) {
                return Double.compare(p1.x(), p2.x());
            }
        });
        computeSpan();
    }
    
    
    private void computeSpan() {
        this.domain = new Domain(points.get(0).x(), points.get(points.size() - 1).x());
        
        double largeY = points.get(0).y();
        double smallY = points.get(0).y();
        
        for(FuncPoint point : points) {
            if (point.y() > largeY) {
                largeY = point.y();
            }
            if (point.y() < smallY) {
                smallY = point.y();
            }
        }
        
        this.range = new Range(smallY, largeY);
    }
    
    
    public boolean inDomain(double x) {
        return domain.inDomain(x);
    }
    
    
    public int indexOfPointSmallerThan(double val) {
        int upperBound=  points.size() - 1;
        int lowerBound = 0;
        int guess = (upperBound + lowerBound) / 2;
        
        while(!((points.get(guess).x() <= val) && (points.get(guess + 1).x() > val))) {
            if(points.get(guess + 1).x() <= val) {
                lowerBound = guess;
                guess = (upperBound + lowerBound) / 2;
            }
            else if (points.get(guess).x() > val) {
                upperBound = guess;
                guess = (upperBound + lowerBound) / 2;
            }
        }
        
        return guess;
    }
    
    
    public String toObjectDBString() {
//        String output = "";
//        int numTabs = 0;
        
        throw new UnsupportedOperationException();
        
//        return output;
    }
    
    
    @Override
    public Domain domain() {
        return domain;
    }
    
    
    public Range range() {
        return this.range;
    }
    
    
    private Variable[] vars = new Variable[1];
    private List<FuncPoint> points = new ArrayList<>();
    
    private Domain domain;
    private Range range;
    
}
